home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / circuits / irsim-ca.2 / irsim-ca / irsim-cap-9.2 / src / ana11 / event.c < prev    next >
C/C++ Source or Header  |  1993-01-15  |  10KB  |  477 lines

  1. /*
  2.  *     ********************************************************************* 
  3.  *     * Copyright (C) 1988, 1990 Stanford University.                     * 
  4.  *     * Permission to use, copy, modify, and distribute this              * 
  5.  *     * software and its documentation for any purpose and without        * 
  6.  *     * fee is hereby granted, provided that the above copyright          * 
  7.  *     * notice appear in all copies.  Stanford University                 * 
  8.  *     * makes no representations about the suitability of this            * 
  9.  *     * software for any purpose.  It is provided "as is" without         * 
  10.  *     * express or implied warranty.  Export of this software outside     * 
  11.  *     * of the United States of America may require an export license.    * 
  12.  *     *********************************************************************
  13.  */
  14.  
  15. /*
  16.  * Logic analyzer event manager.
  17.  */
  18.  
  19. #include <signal.h>
  20. #include <fcntl.h>
  21.  
  22. #ifdef SYS_V
  23. #    include <termio.h>
  24. #    ifdef hpux
  25. #    define    SigBlock( sig )    sigsetmask( sigmask( sig ) )
  26. #    define    SigRelease( sig )    \
  27.             sigsetmask( sigsetmask( 0 ) & ~sigmask( sig ) )
  28. #    else
  29. #    include <stropts.h>
  30. #    define    signal( A, B )        sigset( A, B )
  31. #    define    SigBlock( sig )        sighold( sig )
  32. #    define    SigRelease( sig )    sigrelse( sig )
  33. #    endif
  34. #    define    vfork()        fork()
  35. #else
  36. #    include <sys/ioctl.h>
  37. #    ifndef    sigmask
  38. #    define sigmask( m )        ( 1 << (( m ) - 1 ) )
  39. #    endif
  40. #    define    SigBlock( sig )    sigsetmask( sigmask( sig ) )
  41. #    define    SigRelease( sig )    \
  42.             sigsetmask( sigsetmask( 0 ) & ~sigmask( sig ) )
  43. #endif SYS_V
  44.  
  45. #include <stdio.h>
  46. #include "ana.h"
  47. #include "X11/Xutil.h"
  48. #include "ana_glob.h"
  49. #include "graphics.h"
  50. #include "helper.h"
  51.  
  52.  
  53. #define    WITHINY( Y, Box )    ( (Y <= (Box).bot) and (Y >= (Box).top) )
  54. #define    WITHINX( X, Box )    ( (X >= (Box).left) and (X <= (Box).right) )
  55.  
  56.  
  57. private    int     x_server = 0;
  58. private    Func    FGetEvent = NULL;
  59. private    int     x_helper = 0;        /* process id of helper process */
  60.  
  61.  
  62. private void WindowResize( ev )
  63.   XConfigureEvent  *ev;
  64.   {
  65.     int  ret;
  66.  
  67.     if( ev->width != XWINDOWSIZE or ev->height != YWINDOWSIZE )
  68.       {
  69.     XWINDOWSIZE = ev->width;
  70.     YWINDOWSIZE = ev->height;
  71.     ret = WindowChanges();
  72.       }
  73.   }
  74.  
  75.  
  76. private void WindowExposed( event )
  77.   XExposeEvent  *event;
  78.   {
  79.     BBox  box;
  80.  
  81.     box.left = event->x;
  82.     box.right = event->x + event->width - 1;
  83.     box.top = event->y;
  84.     box.bot = event->y + event->height - 1;
  85.     RedrawWindow( box );
  86.   }
  87.  
  88.  
  89. private void HandleButton( ev )
  90.   XButtonEvent  *ev;
  91.   {
  92.     if( WITHINY( ev->y, scrollBox ) )
  93.     DoScrollBar( ev );
  94.     else if( WITHINY( ev->y, traceBox) )
  95.       {
  96.     if( WITHINX( ev->x, namesBox ) )
  97.         MoveTrace( ev->y );
  98.     else if( WITHINX( ev->x, traceBox ) )
  99.         DoCursor( ev );
  100.     else if( WITHINX( ev->x, cursorBox ) )
  101.         SelectCursTrace( ev->y );
  102.       }
  103.     else if( WITHINY( ev->y, bannerBox ) )
  104.       {
  105.     if( WITHINX( ev->x, iconBox ) )
  106.         IconifyMe();
  107.     else if( WITHINX( ev->x, sizeBox ) )
  108.         ResizeMe();
  109.     else if( WITHINX( ev->x, menuBox ) )
  110.         DoMenu( ev->x, ev->y );
  111.     else
  112.       {
  113.         switch( ev->button & (Button1 | Button2 | Button3) )
  114.           {
  115.         case Button1 : XRaiseWindow( display, window ); break;
  116.         case Button2 : MoveMe( ev->x, ev->y ); break;
  117.         case Button3 : XLowerWindow( display, window ); break;
  118.           }
  119.       }
  120.       }
  121.   }
  122.  
  123.  
  124. private void HandleKey( ev )
  125.   XKeyEvent  *ev;
  126.   {
  127.     char  buff[ 40 ];
  128.     int   nChars, i;
  129.  
  130.     nChars = XLookupString( ev, buff, sizeof( buff ), NULL, NULL );
  131.     for( i = 0; i < nChars; i++ )
  132.       {
  133.     switch( buff[i] )
  134.       {
  135.         case 'i' :
  136.         case 'o' :
  137.         Zoom( buff );
  138.         break;
  139.  
  140.         case 'd' :
  141.         DeltaT( buff );
  142.         break;
  143.  
  144.         case 'm' :
  145.         MoveToTime( buff );
  146.         break;
  147.  
  148.         case 'p' :
  149.         printPS( buff );
  150.         break;
  151.  
  152.         case 'w' :
  153.         SetWidth( buff );
  154.         break;
  155.  
  156.         default:
  157.         XBell( display, 0 );
  158.       }
  159.       }
  160.   }
  161.  
  162.  
  163. public void SendEventTo( f )
  164.   Func  f;
  165.   {
  166.     FGetEvent = f;
  167.   }
  168.  
  169.  
  170.  
  171. private void EventHandler()
  172.   {
  173.     XEvent  event;
  174.  
  175.     if( XEventsQueued( display, QueuedAfterReading ) > 0 )
  176.       {
  177.     do
  178.       {
  179.         XNextEvent( display, &event );
  180.         switch( event.type )
  181.           {
  182.         case ButtonPress :
  183.             if( windowState.tooSmall )
  184.             ;
  185.             else if( FGetEvent != NULL )
  186.             (*FGetEvent)( &event );
  187.             else
  188.             HandleButton( &event.xbutton );
  189.             break;
  190.  
  191.         case KeyPress :
  192.             if( windowState.tooSmall )
  193.             ;
  194.             else if( FGetEvent != NULL )
  195.             (*FGetEvent)( &event.xkey );
  196.             else
  197.             HandleKey( &event.xkey );
  198.              break;
  199.  
  200.         case EnterNotify :
  201.             if( event.xcrossing.window == window )
  202.             WindowCrossed( TRUE );
  203.             break;
  204.  
  205.         case LeaveNotify :
  206.             if( event.xcrossing.window == window )
  207.             WindowCrossed( FALSE );
  208.         break;
  209.  
  210.         case Expose :
  211.             if( event.xexpose.window == iconW )
  212.             RedrawIcon( &event.xexpose );
  213.             else if( windowState.tooSmall )
  214.             RedrawSmallW();
  215.             else if( event.xexpose.window == window )
  216.             WindowExposed( &event.xexpose );
  217.             break;
  218.  
  219.         case ConfigureNotify :
  220.             WindowResize( &event.xconfigure );
  221.             break;
  222.  
  223.         case UnmapNotify :
  224.             windowState.iconified = TRUE;
  225.             break;
  226.  
  227.         case MapNotify :
  228.             windowState.iconified = FALSE;
  229.             windowState.selected = FALSE;
  230.             (void) WindowChanges();
  231.             break;
  232.  
  233.         default : ;
  234.           }
  235.       }
  236.     while( XPending( display ) );
  237.       }
  238.  
  239. #ifdef NEED_HELPER
  240.     if( x_helper != 0 )
  241.     (void) kill( x_helper, SIGINT );
  242. #endif
  243.  
  244. #ifdef SYS_V
  245.     (void) signal( SIGIO, EventHandler );
  246. #endif
  247.  
  248.    }
  249.  
  250.  
  251. private void DisabledEventHandler()
  252.   {
  253.     XEvent  event;
  254.  
  255.     if( XEventsQueued( display, QueuedAfterReading ) > 0 )
  256.       {
  257.     do
  258.       {
  259.         XNextEvent( display, &event );
  260.         switch( event.type )
  261.           {
  262.         case EnterNotify :
  263.             if( event.xcrossing.window == window )
  264.             WindowCrossed( TRUE );
  265.             break;
  266.  
  267.         case LeaveNotify :
  268.             if( event.xcrossing.window == window )
  269.             WindowCrossed( FALSE );
  270.             break;
  271.  
  272.         case Expose :
  273.             if( event.xexpose.window == iconW )
  274.             RedrawIcon( &event );
  275.             else if( windowState.tooSmall )
  276.             RedrawSmallW();
  277.             break;
  278.  
  279.         case ConfigureNotify :
  280.             WindowResize( &event.xconfigure );
  281.             break;
  282.  
  283.         case UnmapNotify :
  284.             windowState.iconified = TRUE;
  285.             break;
  286.  
  287.         case MapNotify :
  288.             windowState.iconified = FALSE;
  289.             windowState.selected = FALSE;
  290.             break;
  291.  
  292.         default : ;
  293.           }
  294.       }
  295.     while( XPending( display ) );
  296.       }
  297.  
  298. #ifdef NEED_HELPER
  299.     if( x_helper != 0 )
  300.     (void) kill( x_helper, SIGINT );
  301. #endif
  302.  
  303. #ifdef SYS_V
  304.     (void) signal( SIGIO, DisabledEventHandler );
  305. #endif
  306.  
  307.    }
  308.  
  309.  
  310. #ifdef NEED_HELPER
  311.  
  312. private int StartHelper( fd )
  313.   int  fd;
  314.   {
  315.     extern char  *cad_bin, *getenv();
  316.     static char  helper_name[] = "anXhelper";
  317.     char         helper_proc[ 256 ];
  318.  
  319.     (void) sprintf( helper_proc, "%s/%s", cad_bin, helper_name );
  320.  
  321.     if( x_helper == 0 )            /* never start 2 processes */
  322.     x_helper = vfork();
  323.  
  324.     if( x_helper == -1 )
  325.       {
  326.     fprintf( stderr, "can't fork helper process\n" );
  327.     x_helper = 0;
  328.     return( FALSE );
  329.       }
  330.     else if( x_helper == 0 )        /* child process */
  331.       {
  332.     if( dup2( fd, 0 ) == -1 )
  333.       {
  334.         fprintf( stderr, "can't dup descriptor\n" );
  335.         _exit( 1 );
  336.       }
  337.  
  338.     (void) execl( helper_proc, helper_name, NULL );
  339.     fprintf( stderr, "can't exec helper process: %s\n", helper_name );
  340.     _exit( 1 );
  341.       }
  342.     (void) sleep( (unsigned) 1 );        /* make sure process started */
  343.  
  344.     if( kill( x_helper, SIGUSR1 ) != 0 )
  345.       {
  346.     x_helper = 0;
  347.     fprintf( stderr, "helper process is dead\n" );
  348.     return( FALSE );
  349.       }
  350.     return( TRUE );
  351.   }
  352.  
  353. #endif NEED_HELPER
  354.  
  355.  
  356.  
  357. public int InitHandler( fd )
  358.   int fd;
  359.   {
  360.     int   flags;
  361.     char  *senv;
  362.  
  363.     x_server = fd;
  364.     (void) signal( SIGIO, EventHandler );
  365.  
  366. #ifdef NEED_HELPER
  367.     return( StartHelper( fd ) );
  368. #endif NEED_HELPER
  369.  
  370. # ifdef SYS_V
  371.     flags = 1;
  372.     if( ioctl( fd, FIOASYNC, &flags ) == -1 )
  373.       {
  374.     fprintf( stderr, "ioctl: can not set FIOASYNC\n" );
  375.     return( FALSE );
  376.       }
  377. # else
  378.     if( (flags = fcntl( fd, F_GETFL, 0 )) == -1 )
  379.       {
  380.     fprintf( stderr, "fctl: can not do F_GETFL\n" );
  381.     return( FALSE );
  382.       }
  383.  
  384.     if( (flags & FASYNC) == 0 )
  385.       {
  386.     if( fcntl( fd, F_SETFL, flags | FASYNC ) == -1 )
  387.       {
  388.         fprintf( stderr, "fctl: can not do F_SETFL\n" );
  389.         return( FALSE );
  390.       }
  391.       }
  392.  
  393. # endif SYS_V
  394.  
  395. # ifdef F_SETOWN
  396.     if( fcntl( fd, F_SETOWN, getpid() ) == -1 )
  397.       {
  398.     fprintf( stderr, "fctl: can not do F_SETOWN\n" );
  399.     return( FALSE );
  400.       }
  401. # endif
  402.  
  403.     return( TRUE );
  404.   }
  405.  
  406.  
  407. public void DisableInput()
  408.   {
  409.     (void) SigBlock( SIGIO );
  410.   }
  411.  
  412.  
  413. public void EnableInput()
  414.   {
  415.     if( XPending( display ) )
  416.       {
  417.     EventHandler();
  418.       }
  419.     else
  420.       {
  421. #ifdef NEED_HELPER
  422.     if( x_helper != 0 )
  423.         (void) kill( x_helper, SIGINT );
  424. #endif
  425.     ;
  426.       }
  427.     (void) SigRelease( SIGIO );
  428.   }
  429.  
  430.  
  431. public void DisableAnalyzer()
  432.   {
  433.     (void) SigBlock( SIGIO );
  434.     (void) signal( SIGIO, DisabledEventHandler );
  435.     (void) SigRelease( SIGIO );
  436.   }
  437.  
  438.  
  439. public void EnableAnalyzer()
  440.   {
  441.     int  change;
  442.  
  443.     DisableInput();
  444.  
  445.     updatePending = FALSE;
  446.  
  447.     if( FGetEvent != NULL )
  448.       {
  449.     (*FGetEvent)( (XEvent *) NULL );    /* let handler know */
  450.     FGetEvent = NULL;
  451.       }
  452.  
  453.     (void) signal( SIGIO, EventHandler );
  454.  
  455.     if( windowState.iconified == 0 and windowState.tooSmall == 0 )
  456.       {
  457.     change = WindowChanges();
  458.     if( not (change & RESIZED) )
  459.       {
  460.         BBox  box;
  461.  
  462.         box.left = box.top = 0;
  463.         box.right = XWINDOWSIZE - 1;
  464.         box.bot = YWINDOWSIZE - 1;
  465.         RedrawWindow( box );
  466.       }
  467.       }
  468.     EnableInput();
  469.   }
  470.  
  471.  
  472. public void TerminateAnalyzer()
  473.   {
  474.     if( x_helper != 0 )
  475.     (void) kill( x_helper, SIGKILL );
  476.   }
  477.